package com.sunda.spmswms.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sunda.spmswms.entity.Werks;
import com.sunda.spmswms.mapper.WerksMapper;
import com.sunda.spmswms.mapper.WhsMapper;
import com.sunda.spmswms.mapper.WorkshopMapper;
import com.sunda.spmswms.service.IWerksService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 工厂信息表 服务实现类
 * </p>
 *
 * @author Wayne
 * @since 2020-11-24
 */
@Service
@Slf4j
public class WerksServiceImpl extends ServiceImpl<WerksMapper, Werks> implements IWerksService {

    @Autowired
    WerksMapper werksMapper;

    @Autowired
    WhsMapper whsMapper;

    @Autowired
    WorkshopMapper workshopMapper;

    @Override
    public List<Werks> getWerksList() {
        return werksMapper.selectList(new QueryWrapper<>());
    }

    @Override
    public List<Map<String, Object>> getWhsByWerks(String werks) {
        return whsMapper.getWhsByWerks(werks);
    }

    @Override
    public List<Map<String, Object>> getWorkshopByWerks(String werks) {
        return workshopMapper.getWorkshopByWerks(werks);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    /** 监听来自 SAP 的公司工厂数据，写入/更新数据库 */
    public void insertWerks(JSONObject doc) {
        try {
            JSONArray array = doc.getJSONArray("Z_SPMS_GETFACTORY_IT_WERKS");
            for(int i = 0; i<array.size(); i++){
                System.out.println("=======" + array.size() + "    " + i + "   SAP 公司工厂信息  " + array.get(i).toString());
                JSONArray json = JSONArray.parseArray(array.get(i).toString());
                if (i > 0){  // i = 0 时存的是表头信息
                    Werks werks = new Werks();
                    werks.setCompanyCode(json.getString(0));
                    werks.setCompanyNameZh(json.getString(1));
                    werks.setCompanyNameEn(json.getString(2));
                    werks.setWerks(json.getString(3));
                    werks.setWerksNameZh(json.getString(4));
                    werks.setWerksNameEn(json.getString(5));
                    werks.setRegioGroup(json.getString(6));
                    werks.setCountry(json.getString(7));
                    werks.setSupplierNo(json.getString(8));

                    if (werksMapper.selectOne(new QueryWrapper<Werks>().eq("werks", werks.getWerks())) == null){
                        werksMapper.insert(werks);
                    }else {
                        werksMapper.update(werks, new QueryWrapper<Werks>().eq("werks", werks.getWerks()));
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            log.info("=========== WerksServiceImpl.insertWerks 执行异常 " + e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
    }

    @Override
    public Werks getWerkInfo(String werksCode) {
        return werksMapper.selectOne(new QueryWrapper<Werks>().eq("werks", werksCode));
    }
}
